/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.command.period;

import apus.command.AbstractCommand;
import apus.entity.Period;
import apus.exception.BusinessException;
import apus.persistence.PeriodDAO;
import apus.services.ServiceLocator;
import java.util.Arrays;
import java.util.List;

/**
 * The <code>RemovePeriodCommand</code> class represents command for removing 
 * period from the database.
 * @author  Maxim Vasilevsky
 * @author  Roman Dyatkovsky
 * @since APUS v0.4
 * @see AbstractCommand
 */
public class RemovePeriodCommand extends AbstractCommand{
    
    private final static String PARAM_ID = "Period ID";   
    private final static String[] paramsNames = { PARAM_ID };
    
    public RemovePeriodCommand() {
        super("Remove period");
    }
    
    @Override
    public void execute() {
        int ID;
        String stringId = params.get(PARAM_ID);
        
        try{
            ID = Integer.parseInt(stringId);
        } catch(NumberFormatException e){
            throw new BusinessException("Wrong ID format: "+stringId, e);
        }
        
        PeriodDAO periodDAO = ServiceLocator.getService(PeriodDAO.class);     
        Period period = periodDAO.read(ID);
        
        if (period == null) {
            throw new BusinessException("Period not found");
        }
        
        periodDAO.delete(period);
    }
    
    
    @Override
    public List<String> getParamNames() {
        return Arrays.asList(paramsNames);
    }
    
}
